import socket

# 创建socket对象
sk = socket.socket()

# 绑定IP和端口
sk.bind(("127.0.0.1", 8000))  # 本地地址和端口

# 监听
sk.listen()


def rihan():
    return "欢迎访问日韩板块儿"

def oumei():
    return "欢迎访问欧美版块儿"

# 等待接入
while True:
    conn, addr = sk.accept()

    # 接受数据
    data = conn.recv(2048).decode('utf-8')
    url = data.split()[1]
    print(url)  # 空格分割

    # 返回数据
    conn.send(b"HTTP/1.1 200 OK\r\ncontent-type:text/html; charset=utf-8\r\n\r\n")

    if url == "/yemian1":
        tr = rihan()

    elif url == "/yemian2":
        tr = oumei()

    else:
        tr = "404 not found"

    conn.send(tr.encode("utf-8"))

    # 断开连接
    conn.close()

